<?php
//写文件
function writeFile($filename,$data,$method='rb+',$iflock=1,$check=1,$chmod=1){
	touch($filename);
	$handle = fopen($filename,$method);
	$iflock && flock($handle,LOCK_EX);
	fwrite($handle,$data);
	$method=='rb+' && ftruncate($handle,strlen($data));
	fclose($handle);
	$chmod && @chmod($filename,0777);
}

//输出变量为字符串
function varToStr($vari,$t = null) {
	switch (gettype($vari)) {
		case 'string':
			return "'".str_replace(array("\\","'"),array("\\\\","\'"),$vari)."'";
		case 'array':
			$output = "array(\r\n";
			foreach ($vari as $key => $value) {
				$output .= $t."\t".varToStr($key,$t."\t").' => '.varToStr($value,$t."\t");
				$output .= ",\r\n";
			}
			$output .= $t.')';
			return $output;
		case 'boolean':
			return $vari ? 'true' : 'false';
		case 'NULL':
			return 'NULL';
		case 'integer':
		case 'double':
		case 'float':
			return "'".(string)$vari."'";
	 }
	 return 'NULL';
}

function writeGroupsCache(){
	global $db;
	$rows=$db->row_select("groups","groupname<>''",0,"groupid,groupname,popedom,starnum,color","groupid");
	$groups=array();
	foreach($rows as $row){
		$groups["{$row['groupid']}"]=$row;
	}
	$row_str='';
	$rows_normal=$db->row_select("groups","groupid<100 and groupname<>''",0,"groupid,groupname,popedom,starnum,color","groupid");
	foreach($rows_normal as $row){
		$row_str.="<option value={$row['groupid']}>{$row['groupname']}</option>";
	}
	$row_str2='';
	$rows_system=$db->row_select("groups","groupid>".GROUP_GUESS." and groupname<>''",0,"groupid,groupname,popedom,starnum,color","groupid");
	foreach($rows_system as $row){
		$row_str2.="<option value={$row['groupid']}>{$row['groupname']}</option>";
	}

	$str="<?php \r\n \$cache_groups = ".varToStr($groups).";\r\n \$cache_groups1option=\"{$row_str}\";\r\n \$cache_groups2option=\"{$row_str2}\";\r\n ?>";
	writeFile(getCacheFilePath("groups.php"),$str);
}

function writeSettingsCache(){
	global $db;
	$row=getSettings();
	$apikey=$row['apikey'];
	$tmparr=array();
	if(!empty($apikey)){
		$row['apikey']=array();
		$tmpapis=explode("\n",$apikey);
		foreach($tmpapis as $tmpapi){
			$tmpapikv=explode(":",$tmpapi);
			if(is_array($tmpapikv) && !empty($tmpapikv[0]) && !empty($tmpapikv[1])){
				$row['apikey'][$tmpapikv[0]]=trim($tmpapikv[1]);
			}
		}
	}
	
	$str="<?php \r\n \$cache_settings = ".varToStr($row)."; \r\n?>";
	writeFile(getCacheFilePath("settings.php"),$str);
}

function writeCreditsCache(){
	global $db;
	$outs=array();
	$rows=$db->row_select("credits","isuse=1",0,"*","id");
	foreach($rows as $row){
		$outs[$row['creditscode']] = $row;
	}
	$str="<?php \r\n \$cache_credits = ".varToStr($outs)."; \r\n?>";
	writeFile(getCacheFilePath("credits.php"),$str);
}

function writeForumsCache(){
	global $db;
	$forums=array();
	$forums_tree="\r\n";
	$forums_option="";
	$forums_option2="";
	/****1*****/
	$rows1=$db->row_select("forums","pid=0",0,"*","ordernum,id");
	for($i=0;$i<count($rows1);$i++){
		$row1=$rows1[$i];
		$forums[$row1['id']]=$row1;
		$forums[$row1['id']]['childfid']=array();
		$forums[$row1['id']]['admins']=$row1['admins'];
		$forums[$row1['id']]['padmins'] = '';
		$forums_option.="<option value=\"{$row1['id']}\">&gt;&gt; {$row1[forumname]}</option>";
		$forums_option2.="<optgroup label=\"&gt;&gt;{$row1[forumname]}\">";
		/*****2****/
		$rows2=$db->row_select("forums","pid={$row1['id']}",0,"*","ordernum,id");
		for($j=0;$j<count($rows2);$j++){
			$row2=$rows2[$j];
			$forums[$row2['id']]=$row2;
			$forums[$row2['id']]['childfid']=array();
			$forums[$row2['id']]['admins']= $row2['admins'];
			$forums[$row2['id']]['padmins'] = $row1['admins'];
			$forums_tree.="array_push(\$cache_forums['$row2[pid]']['childfid'],'$row2[id]');\r\n";
			$forums_option.="<option value=\"{$row2['id']}\"> &nbsp;|- {$row2[forumname]}</option>";
			$forums_option2.="<option value=\"{$row2['id']}\"> &nbsp;|- {$row2[forumname]}</option>";
			/*****3******/
			$rows3=$db->row_select("forums","pid={$row2['id']}",0,"*","ordernum,id");
			for($k=0;$k<count($rows3);$k++){
				$row3=$rows3[$k];
				$forums[$row3['id']]=$row3;
				$forums[$row3['id']]['childfid']=array();
				$forums[$row3['id']]['admins']= $row3['admins'];
				$forums[$row3['id']]['padmins'] = $row1['admins']."".$row2['admins'];
				$forums_tree.="array_push(\$cache_forums['$row3[pid]']['childfid'],'$row3[id]');\r\n";
				$forums_option.="<option value=\"{$row3['id']}\">&nbsp;&nbsp; &nbsp; &nbsp;|-  {$row3[forumname]}</option>";
				$forums_option2.="<option value=\"{$row3['id']}\">&nbsp;&nbsp; &nbsp; &nbsp;|-  {$row3[forumname]}</option>";
			}
		}
		$forums_option2.="</optgroup>";
	}
	$str="<?php\r\n";
	$str.="\$cache_forums = ".varToStr($forums)."; \r\n";
	$str.="{$forums_tree}\r\n";
	$str.="\$cache_forumsoption = ".varToStr($forums_option)."; \r\n";
	$str.="\$cache_forumsoption2 = ".varToStr($forums_option2)."; \r\n";
	$str.="?>";
	writeFile(getCacheFilePath("forums.php"),$str);
}

function writeAnnouncesCache(){
	global $db;
	$curtime=time();
	$rows=$db->row_select("announces","stoptime>{$curtime} or stoptime=0",0,"*","ordernum,id desc");
	$newrows=array();
	$anc_tree="";
	$anc_fid_arr=array();
	foreach($rows as $row){
		unset($row['content']);
		unset($row['ordernum']);
		$newrows[$row['id']] = $row;
		if(!in_array($row['targets'] , $anc_fid_arr)){
			$anc_tree.= "\$cache_announces_tree['$row[targets]'] = array();\r\n";
			array_push($anc_fid_arr, $row['targets']);
		}
		$anc_tree.="array_push(\$cache_announces_tree['$row[targets]'],'$row[id]');\r\n";

	}
	$str="<?php\r\n";
	$str.="\$cache_announces = ".varToStr($newrows)."; \r\n";
	$str.="{$anc_tree}\r\n";
	$str.="?>";
	writeFile(getCacheFilePath("announces.php"),$str);
}


function writeAdsCache(){
	global $db;
	$curtime=time();
	$rows = $db->row_select("ads","isuse=1 and (stoptime>{$curtime} or stoptime=0)",0,"*","ordernum");
	$cache_ads=array();
	foreach($rows as $row){
		$adtype=$row['adtype'];
		unset($row['starttime']);
		unset($row['isuse']);
		unset($row['ordernum']);
		unset($row['adtype']);
		unset($row['id']);
		if(empty($cache_ads[$adtype])){
			$cache_ads[$adtype]=array();
		}
		array_push($cache_ads[$adtype],$row);
	}
	$str="<?php \r\n \$cache_ads = ".varToStr($cache_ads)."; \r\n\r\n ?>";
	writeFile(getCacheFilePath("ads.php"),$str);
}

function writeLinksCache(){
	global $db;
	$rows = $db->row_select("links","",0,"*","ordernum");
	$link_logo=array();
	$link_text=array();
	foreach($rows as $row){
		if(empty($row['logo'])){
			array_push($link_text,$row);
		}else{
			array_push($link_logo,$row);
		}
	}
	$str="<?php \r\n \$cache_links_text = ".varToStr($link_text)."; \r\n\r\n \$cache_links_logo = ".varToStr($link_logo)."; \r\n?>";
	writeFile(getCacheFilePath("links.php"),$str);
}

function writeStampsCache(){
	$cache_stampsoption = '<option value="1">优秀</option><option value="2">原创</option><option value="3">推荐</option><option value="4">版主推荐</option><option value="5">爆料</option><option value="6">美图</option>';
	$str="<?php \r\n \$cache_stampsoption = '<option value=\"1\">优秀</option><option value=\"2\">原创</option><option value=\"3\">推荐</option><option value=\"4\">版主推荐</option><option value=\"5\">爆料</option><option value=\"6\">美图</option>'; \r\n?>";
	writeFile(getCacheFilePath("stamps.php"),$str);
}

function writePluginsCache(){
	
}

function writeTemplatesCache(){

}




?>